C Programming Test - Answer Key
1. What is an array? Give an example with printing an array (5 marks)
An array is a collection of elements, all of the same type, stored in contiguous memory locations. Arrays allow indexed access, where each element can be referenced using its position in the array (starting at index 0). This makes it easy to store and access multiple values.
Example Code:
Example Code:
#include <stdio.h>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Output: 1 2 3 4 5
2. Write any sorting algorithm steps with an example (5 marks)
Bubble Sort is one of the simplest sorting algorithms. It works by repeatedly swapping adjacent elements if they are in the wrong order. The process is repeated until the array is sorted.
Steps:
Steps:
- Compare the first two elements.
- Swap them if the first is larger than the second.
- Move to the next element and repeat until the end of the array.
- Repeat the entire process until no swaps are needed.
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
3. Write counting from 1-20 using while, do-while, and for loop (10 marks)
To count from 1 to 20, the while, do-while, and for loops can be used.
While Loop:
While Loop:
#include <stdio.h>
int main() {
int i = 1;
while (i <= 20) {
printf("%d ", i);
i++;
}
return 0;
}
Do-While Loop:
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 20);
return 0;
}
For Loop:
#include <stdio.h>
int main() {
for (int i = 1; i <= 20; i++) {
printf("%d ", i);
}
return 0;
}
4. What is a pointer? Write the syntax of malloc() (5 marks)
A pointer is a variable that stores the memory address of another variable. Pointers are essential for dynamic memory allocation.
Syntax of malloc():
Syntax of malloc():
ptr = (castType*) malloc(size_in_bytes);
Example:
int* ptr = (int*) malloc(5 * sizeof(int));
5. Get the sum of these two numbers (15 and 10) using a function (5 marks)
In this task, we create a function that takes two integers as input and returns their sum.
Example Code:
Example Code:
#include <stdio.h>
int getSum(int a, int b) {
return a + b;
}
int main() {
int a = 15, b = 10;
int sum = getSum(a, b);
printf("Sum: %d", sum);
return 0;
}
6. What is a string? Write a program to reverse a string given in the input (10 marks)
A string in C is a sequence of characters terminated by a null character (
Example Code to Reverse a String:
'\0'
).
Example Code to Reverse a String:
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
int len, i, j;
printf("Enter a string: ");
gets(str);
len = strlen(str);
for (i = len - 1, j = 0; i >= 0; i--, j++) {
rev[j] = str[i];
}
rev[j] = '\0';
printf("Reversed string: %s", rev);
return 0;
}
7. Write the 5 differences between if-else and switch (10 marks)
- Syntax:
if-else
uses conditions;switch
uses cases for a single expression. - Data Types:
if-else
supports various data types;switch
is limited to integers and characters. - Execution:
switch
is more efficient when comparing multiple values compared toif-else
. - Readability:
switch
is more readable when dealing with multiple conditions. - Use Cases:
if-else
is used for complex conditions;switch
is preferred when comparing a single variable.